#include "tbworker.h"

#include "vmd5.h"

void MD5String(char *src,char *digest);

int get_http_recv(RecvBlockQueue& recvqueue, const char* addrinfo, const char* inbuf, int inlen)
{
    CHttpBlock* block = new CHttpBlock(addrinfo);

    if(NULL == block) return 0;

    CHttpBuffer recvmsg(inbuf, inlen);

    int result = recvmsg.ToBlock(block);

    if(result <= 0) delete block;
    else recvqueue.Push(block);

    return result;
}

TBRequest::TBRequest()
: _url("/router/rest?sign=")
, _format("xml")
, _sign_method("md5")
{
    _v = "2.0";
}

string& TBRequest::ToRequest()
{
    char curtime[20] = { 0 }; get_cur_time(curtime, 20);

    _request = _url;

    string outbuf = _secret;
    outbuf += "app_key"; outbuf += _app_key;
    outbuf += "fields"; outbuf += _fields;
    outbuf += "format"; outbuf += _format;
    outbuf += "method"; outbuf += _method;
    outbuf += "session"; outbuf += _session;
    outbuf += "sign_method"; outbuf += _sign_method;
    outbuf += "timestamp"; outbuf += curtime;
    outbuf += "v"; outbuf += _v; outbuf += _secret;

    char md5buf[33] = {0}; MD5String((char*)outbuf.c_str(), md5buf);

    _request += md5buf; _request += "&timestamp=";

    curtime[10] = '\0'; _timestamp += curtime; _timestamp += "+";
    curtime[13] = '\0'; _timestamp += &curtime[11]; _timestamp += "%3A";
    curtime[16] = '\0'; _timestamp += &curtime[14]; _timestamp += "%3A";
    _timestamp += &curtime[17];

    _request += _timestamp;
    
    _request += "&v="; _request += _v;
    _request += "&app_key="; _request += _app_key;
    _request += "&method="; _request += _method;
    _request += "&format="; _request += _format;
    _request += "&session="; _request += _session;
    _request += "&fields="; _request += _fields;

    return _request;
}

CTBSyncWorker::CTBSyncWorker(const char* appkey, const char* secret, bool istest)
: m_appkey(appkey)
, m_secret(secret)
{
    if(istest)
    {
        m_host =  "gw.api.tbsandbox.com";

        SetSocketInfo("110.75.120.25", 80);
    }
    else
    {
        m_host = "gw.api.taobao.com";

        SetSocketInfo("110.75.39.39", 80);
    }

    sprintf(m_hostheader, "%s:%d\0", m_socketinfo._ip, m_socketinfo._port);
}

void CTBSyncWorker::SetRequest(TBRequest& param, string fields, string method, string session, string httpmethod)
{
    param._httpmethod = httpmethod;

    param._app_key = m_appkey;
    param._secret = m_secret;
    
    param._fields = fields;
    param._method = method;
    param._session = session;
}

bool CTBSyncWorker::Work(TBRequest& request, TBResponse& response)
{
    HttpLine httpline;
    httpline._method = request._httpmethod;
    httpline._url = request.ToRequest();

    httpline._url = "/cowsrv";
    SetSocketInfo("10.10.208.170", 8060);

    CHttpBlock sendblock; CHttpBuffer sendbuf;
    en_http_line(sendblock, httpline);
    en_http_header(sendblock, "Host", m_hostheader);
    en_http_header(sendblock, "Connection", "Keep-Alive");

    en_http_body(sendblock, "1234567890");

    if(!sendblock.ToBuffer(&sendbuf))
        return false;

    if(!SyncWork(sendbuf.GetByte(), sendbuf.GetSize()))
        return false;

    RecvBlock* recvblock = NULL;

    if(0 != m_recvqueue.Pop(recvblock))
        return false;

    DEL_SINGLE(recvblock);

    return true;
}

int CTBSyncWorker::OnRecvMsg(const char* inbuf, int inlen)
{
    return get_http_recv(m_recvqueue, "", inbuf, inlen);
}
